package org.devgframwork.flowsystem.api.admin.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.devgframwork.flowsystem.api.admin.user.dao.UserMapper;
import org.devgframwork.flowsystem.api.admin.user.dao.UserRoleMapper;
import org.devgframwork.flowsystem.api.admin.user.entity.po.User;
import org.devgframwork.flowsystem.api.admin.user.entity.po.UserRole;
import org.devgframwork.flowsystem.api.admin.user.entity.vo.UserVo;
import org.devgframwork.flowsystem.api.admin.user.service.UserService;
import org.devgframwork.platform.model.ResultModel;
import org.devgframwork.platform.utils.CommonUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author duheng
 * @Date 2021/11/13 17:21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private HttpServletRequest request;

    @Override
    public ResultModel login(UserVo userVo) {
        if (CommonUtil.isNullOrEmpty(userVo.getLoginName()) || CommonUtil.isNullOrEmpty(userVo.getPassword())) {
            return ResultModel.newFail("用户名或密码不能为空");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("LOGIN_NAME", userVo.getLoginName());
        User resultDto = userMapper.selectOne(queryWrapper);
        if (null == resultDto || (!CommonUtil.equals(resultDto.getPassword(), userVo.getPassword()))) {
            return ResultModel.newFail("用户名或密码错误");
        }
        if (null != resultDto.getIsDelete() && resultDto.getIsDelete().equals(1)) {
            return ResultModel.newFail("用户已删除");
        }
        /**
         * 分页测试
         *
         *  Page<User> page = new Page(1, 10);
         *  Page<User> userList = userMapper.getUserList(page,userVo);
         *
         */

        long userId = resultDto.getUserId();
        String tokenKey = UserServiceImpl.prefixKey + userId;
        Map<String, Object> result = new HashMap<>(3);
        String token = UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(tokenKey, token, 5, TimeUnit.MINUTES);
        result.put("token", token);
        result.put("userId", userId);
        result.put("userName", resultDto.getUserName());
        return ResultModel.newSuccess(result);
    }

    @Override
    public long getUserId() {
        String userId = request.getHeader("userId");
        return CommonUtil.isNullOrEmpty(userId) ? 0L : Long.valueOf(userId);
    }

    @Override
    public List<Long> getUserRoleIds() {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", getUserId());
        queryWrapper.eq("IS_DELETE", 0);
        List<UserRole> userRoleDtoList = userRoleMapper.selectList(queryWrapper);
        return userRoleDtoList.stream().map(e -> e.getRoleId()).collect(Collectors.toList());
    }
}
